package com.tiger.log.server.service.impl;

import com.tiger.log.common.constant.LogConstant;
import com.tiger.log.common.constant.SaveProperties;
import com.tiger.log.common.entity.SysRecordLog;
import com.tiger.log.server.mapper.SysRecordLogMapper;
import com.tiger.log.server.service.SysRecordLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ClassName SysRecordLogServiceImpl
 * @Description TODO
 * @Author huxuehao
 **/
@Service
public class SysRecordLogServiceImpl implements SysRecordLogService {

    private final String TABLE_NAME = "sys_record_log";
    private final SysRecordLogMapper sysRecordLogMapper;
    private final SaveProperties logProperties;

    public SysRecordLogServiceImpl(SysRecordLogMapper sysRecordLogMapper, SaveProperties logProperties) {
        this.sysRecordLogMapper = sysRecordLogMapper;
        this.logProperties = logProperties;
    }

    @Override
    public List<SysRecordLog> getAll() {
        return sysRecordLogMapper.selectAll();
    }

    @Override
    public SysRecordLog getById(String id) {
        return sysRecordLogMapper.selectById(id);
    }

    @Override
    public List<SysRecordLog> getByBusinessId(String businessId) {
        return sysRecordLogMapper.selectByBusinessId(businessId);
    }

    @Override
    public List<SysRecordLog> getByClassify(String classify) {
        return sysRecordLogMapper.selectByClassify(classify);
    }

    @Override
    public List<SysRecordLog> getByCode(String code) {
        return sysRecordLogMapper.selectByCode(code);
    }

    @Override
    public int deleteAll() {
        return sysRecordLogMapper.deleteAll();
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return sysRecordLogMapper.deleteByIds(ids);
    }

    @Override
    public int deleteByBusinessIds(List<String> businessIds) {
        return sysRecordLogMapper.deleteByBusinessIds(businessIds);
    }

    @Override
    public int deleteByClassifies(List<String> classifies) {
        return sysRecordLogMapper.deleteByClassifies(classifies);
    }

    @Override
    @Async("logCustomThreadPoolExecutor")
    public Boolean asyncGenLog(SysRecordLog recordLog) {
        if (validRecordLog(recordLog)) {
            return sysRecordLogMapper.add(recordLog) > 0 ? true : false;
        }
        return false;
    }

    @Override
    public Boolean validDayCycle() throws ParseException {
        String dayCycleStr = sysRecordLogMapper.selectDayCycle();
        if (dayCycleStr == null || "".equals(dayCycleStr)) {
            return false;
        }
        String[] split = dayCycleStr.split(",");
        if (split[0].equals(split[1])) {
            return false;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(LogConstant.TIME_FORMAT);
        long minTime = dateFormat.parse(split[0]).getTime();
        long maxTime = dateFormat.parse(split[1]).getTime();

        return maxTime - minTime >= 1000 * 60 * 60 * 24 * logProperties.getSaveDays() ? true : false;
    }

    @Override
    public Boolean doRotate(String tableSuffix) throws ParseException {
        if (this.validDayCycle()) {
            sysRecordLogMapper.copyTable(TABLE_NAME + tableSuffix);
            sysRecordLogMapper.delRecordsIFExist(TABLE_NAME + tableSuffix);
            return true;
        }
        return false;
    }

    /* 验证RecordLog是否合法 */
    private boolean validRecordLog(SysRecordLog recordLog) {
        return recordLog.getBusinessId() != null;
    }
}
